<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>NeuZephyr: NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode Class Reference</title>
<link rel="icon" href="NZ_logo2.png" type="image/x-icon" />
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="NZ_logo2.png"/></td>
  <td id="projectalign">
   <div id="projectname">NeuZephyr
   </div>
   <div id="projectbrief">Simple DL Framework</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>Class&#160;List</span></a></li>
      <li><a href="classes.html"><span>Class&#160;Index</span></a></li>
      <li><a href="inherits.html"><span>Class&#160;Hierarchy</span></a></li>
      <li><a href="functions.html"><span>Class&#160;Members</span></a></li>
    </ul>
  </div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){ initResizable(false); });
/* @license-end */
</script>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><b>NeuZephyr</b></li><li class="navelem"><a class="el" href="namespace_neu_zephyr_1_1_nodes.html">Nodes</a></li><li class="navelem"><a class="el" href="namespace_neu_zephyr_1_1_nodes_1_1_loss.html">Loss</a></li><li class="navelem"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node.html">BinaryCrossEntropyNode</a></li>  </ul>
</div>
</div><!-- top -->
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node-members.html">List of all members</a>  </div>
  <div class="headertitle"><div class="title">NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode Class Reference</div></div>
</div><!--header-->
<div class="contents">

<p>Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph.  
 <a href="#details">More...</a></p>
<div class="dynheader">
Inheritance diagram for NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode:</div>
<div class="dyncontent">
<div class="center"><img src="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node__inherit__graph.png" border="0" usemap="#a_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_inherit__map" alt="Inheritance graph"/></div>
<map name="a_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_inherit__map" id="a_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_inherit__map">
<area shape="rect" title="Represents the Binary Cross&#45;Entropy (BCE) loss function node in a computational graph." alt="" coords="12,171,190,213"/>
<area shape="rect" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html" title="Base class for loss function nodes in a computational graph." alt="" coords="5,80,197,123"/>
<area shape="poly" title=" " alt="" coords="104,138,104,170,98,170,98,138"/>
<area shape="rect" href="class_neu_zephyr_1_1_nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph." alt="" coords="16,5,186,32"/>
<area shape="poly" title=" " alt="" coords="104,48,104,80,98,80,98,48"/>
</map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<div class="dynheader">
Collaboration diagram for NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode:</div>
<div class="dyncontent">
<div class="center"><img src="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node__coll__graph.png" border="0" usemap="#a_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_coll__map" alt="Collaboration graph"/></div>
<map name="a_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_coll__map" id="a_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_coll__map">
<area shape="rect" title="Represents the Binary Cross&#45;Entropy (BCE) loss function node in a computational graph." alt="" coords="12,171,190,213"/>
<area shape="rect" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html" title="Base class for loss function nodes in a computational graph." alt="" coords="5,80,197,123"/>
<area shape="poly" title=" " alt="" coords="104,138,104,170,98,170,98,138"/>
<area shape="rect" href="class_neu_zephyr_1_1_nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph." alt="" coords="16,5,186,32"/>
<area shape="poly" title=" " alt="" coords="104,48,104,80,98,80,98,48"/>
</map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pub-methods" name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:a909fb24c4dac76c49910cdd7cfce7f00" id="r_a909fb24c4dac76c49910cdd7cfce7f00"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a909fb24c4dac76c49910cdd7cfce7f00">BinaryCrossEntropyNode</a> (<a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html">Node</a> *input1, <a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html">Node</a> *input2)</td></tr>
<tr class="memdesc:a909fb24c4dac76c49910cdd7cfce7f00"><td class="mdescLeft">&#160;</td><td class="mdescRight">Constructor to initialize a <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node.html" title="Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph.">BinaryCrossEntropyNode</a></code> for computing the Binary Cross-Entropy loss.  <br /></td></tr>
<tr class="separator:a909fb24c4dac76c49910cdd7cfce7f00"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a4910934419844d1cf5b6fe2252beed88" id="r_a4910934419844d1cf5b6fe2252beed88"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a4910934419844d1cf5b6fe2252beed88">forward</a> () override</td></tr>
<tr class="memdesc:a4910934419844d1cf5b6fe2252beed88"><td class="mdescLeft">&#160;</td><td class="mdescRight">Computes the Binary Cross-Entropy (BCE) loss in the forward pass.  <br /></td></tr>
<tr class="separator:a4910934419844d1cf5b6fe2252beed88"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a8e684013f81ef2431f2afcb551d00b0e" id="r_a8e684013f81ef2431f2afcb551d00b0e"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a8e684013f81ef2431f2afcb551d00b0e">backward</a> () override</td></tr>
<tr class="memdesc:a8e684013f81ef2431f2afcb551d00b0e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the backward pass.  <br /></td></tr>
<tr class="separator:a8e684013f81ef2431f2afcb551d00b0e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="inherit_header pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td colspan="2" onclick="javascript:dynsection.toggleInherit('pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node')"><img src="closed.png" alt="-"/>&#160;Public Member Functions inherited from <a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html">NeuZephyr::Nodes::Standard::OutputNode</a></td></tr>
<tr class="memitem:a25cd49675b51fcc993611dd9aec03b4d inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node" id="r_a25cd49675b51fcc993611dd9aec03b4d"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html#a25cd49675b51fcc993611dd9aec03b4d">OutputNode</a> (<a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html">Node</a> *input)</td></tr>
<tr class="memdesc:a25cd49675b51fcc993611dd9aec03b4d inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Constructor to initialize an <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html" title="Base class for loss function nodes in a computational graph.">OutputNode</a></code> with a given input node.  <br /></td></tr>
<tr class="separator:a25cd49675b51fcc993611dd9aec03b4d inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a953ccc48d72a5e23501a6bc265415bad inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node" id="r_a953ccc48d72a5e23501a6bc265415bad"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html#a953ccc48d72a5e23501a6bc265415bad">forward</a> () override</td></tr>
<tr class="memdesc:a953ccc48d72a5e23501a6bc265415bad inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Forward pass for the <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html" title="Base class for loss function nodes in a computational graph.">OutputNode</a></code>.  <br /></td></tr>
<tr class="separator:a953ccc48d72a5e23501a6bc265415bad inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2a9f8963286cd2e59fedc2c3e731835a inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node" id="r_a2a9f8963286cd2e59fedc2c3e731835a"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html#a2a9f8963286cd2e59fedc2c3e731835a">backward</a> () override</td></tr>
<tr class="memdesc:a2a9f8963286cd2e59fedc2c3e731835a inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Backward pass for the <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html" title="Base class for loss function nodes in a computational graph.">OutputNode</a></code>.  <br /></td></tr>
<tr class="separator:a2a9f8963286cd2e59fedc2c3e731835a inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ab6ef4ccdda9297210927b2055b5fddfc inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node" id="r_ab6ef4ccdda9297210927b2055b5fddfc"><td class="memItemLeft" align="right" valign="top">Tensor::value_type&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html#ab6ef4ccdda9297210927b2055b5fddfc">getLoss</a> () const</td></tr>
<tr class="memdesc:ab6ef4ccdda9297210927b2055b5fddfc inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Retrieves the loss value stored in the <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node.html" title="Base class for loss function nodes in a computational graph.">OutputNode</a></code>.  <br /></td></tr>
<tr class="separator:ab6ef4ccdda9297210927b2055b5fddfc inherit pub_methods_class_neu_zephyr_1_1_nodes_1_1_standard_1_1_output_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="inherit_header pub_methods_class_neu_zephyr_1_1_nodes_1_1_node"><td colspan="2" onclick="javascript:dynsection.toggleInherit('pub_methods_class_neu_zephyr_1_1_nodes_1_1_node')"><img src="closed.png" alt="-"/>&#160;Public Member Functions inherited from <a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html">NeuZephyr::Nodes::Node</a></td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph. </p>
<p>The <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node.html" title="Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph.">BinaryCrossEntropyNode</a></code> class computes the Binary Cross-Entropy loss between two input tensors. BCE is typically used in binary classification tasks to measure the difference between predicted probabilities and the true binary labels. The loss is calculated as: </p><div class="fragment"><div class="line">BCE(x, y) = -1/n * Σ [y_i * log(x_i) + (1 - y_i) * log(1 - x_i)]</div>
</div><!-- fragment --><p> where <code>x</code> represents the predicted probabilities (output of the model), and <code>y</code> represents the true binary labels (either 0 or 1). The loss is computed element-wise for each pair of corresponding values in the tensors.</p>
<p>Key features:</p><ul>
<li><b>Forward Pass</b>: Calculates the Binary Cross-Entropy loss between the two input tensors, storing the result in the output tensor. The BCE loss is computed for each element, and the results are aggregated to produce the final loss.</li>
<li><b>Backward Pass</b>: Computes the gradients of the BCE loss with respect to the input tensors for use in backpropagation. The gradients are propagated only if the output tensor requires gradients.</li>
<li><b>Shape Compatibility</b>: Ensures that both input tensors have the same shape. An exception is thrown if the shapes do not match.</li>
<li><b>Efficient <a class="el" href="namespace_neu_zephyr_1_1_nodes_1_1_computation.html" title="Contains classes and functionality for computation nodes in a neural network or computational graph.">Computation</a></b>: The forward and backward passes are optimized for parallel execution using CUDA to handle large datasets efficiently.</li>
</ul>
<p>This class is part of the <code><a class="el" href="namespace_neu_zephyr_1_1_nodes.html" title="Contains classes and functionality for nodes in a neural network or computational graph.">NeuZephyr::Nodes</a></code> namespace and is used in models for binary classification tasks.</p>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>The <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node.html" title="Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph.">BinaryCrossEntropyNode</a></code> requires two input nodes: the predicted probabilities (<code>input1</code>) and the true binary labels (<code>input2</code>).</li>
<li>Both input tensors must have the same shape, and the BCE loss is calculated element-wise across the tensors.</li>
<li>The <code><a class="el" href="#a4910934419844d1cf5b6fe2252beed88" title="Computes the Binary Cross-Entropy (BCE) loss in the forward pass.">forward()</a></code> method computes the BCE loss on the GPU, and the <code><a class="el" href="#a8e684013f81ef2431f2afcb551d00b0e" title="Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the b...">backward()</a></code> method computes the gradients of the BCE loss.</li>
<li>The loss is stored in the <code>loss</code> attribute, which is updated during the forward pass.</li>
<li>The gradients are stored in the <code>grad</code> attribute of the output tensor during the backward pass.</li>
</ul>
</dd></dl>
<h3><a class="anchor" id="autotoc_md56"></a>
Usage Example:</h3>
<div class="fragment"><div class="line"><span class="comment">// Example: Using BinaryCrossEntropyNode in a computational graph</span></div>
<div class="line">InputNode input1({3, 3}, <span class="keyword">true</span>);  <span class="comment">// Create the first input node (predicted probabilities)</span></div>
<div class="line">InputNode input2({3, 3}, <span class="keyword">true</span>);  <span class="comment">// Create the second input node (true labels)</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">float</span> data1[] = {0.9f, 0.2f, 0.8f, 0.1f, 0.5f, 0.7f, 0.3f, 0.9f, 0.6f};  <span class="comment">// Sample predicted values</span></div>
<div class="line"><span class="keywordtype">float</span> data2[] = {1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f};  <span class="comment">// Sample true labels</span></div>
<div class="line">input1.output-&gt;copyData(data1);  <span class="comment">// Copy data to the first input tensor</span></div>
<div class="line">input2.output-&gt;copyData(data2);  <span class="comment">// Copy data to the second input tensor</span></div>
<div class="line"> </div>
<div class="line"><a class="code hl_function" href="#a909fb24c4dac76c49910cdd7cfce7f00">BinaryCrossEntropyNode</a> bce_node(&amp;input1, &amp;input2);  <span class="comment">// Create the Binary Cross-Entropy node</span></div>
<div class="line">bce_node.forward();  <span class="comment">// Perform the forward pass and compute the BCE loss</span></div>
<div class="line">bce_node.backward();  <span class="comment">// Perform the backward pass and compute gradients</span></div>
<div class="line"> </div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;BCE Loss: &quot;</span> &lt;&lt; bce_node.getLoss() &lt;&lt; std::endl;  <span class="comment">// Print the computed BCE loss</span></div>
<div class="ttc" id="aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_html_a909fb24c4dac76c49910cdd7cfce7f00"><div class="ttname"><a href="#a909fb24c4dac76c49910cdd7cfce7f00">NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode::BinaryCrossEntropyNode</a></div><div class="ttdeci">BinaryCrossEntropyNode(Node *input1, Node *input2)</div><div class="ttdoc">Constructor to initialize a BinaryCrossEntropyNode for computing the Binary Cross-Entropy loss.</div><div class="ttdef"><b>Definition</b> <a href="_nodes_8cu_source.html#l00525">Nodes.cu:525</a></div></div>
</div><!-- fragment --><dl class="section see"><dt>See also</dt><dd><a class="el" href="#a4910934419844d1cf5b6fe2252beed88" title="Computes the Binary Cross-Entropy (BCE) loss in the forward pass.">forward()</a> for the Binary Cross-Entropy computation in the <a class="el" href="#a4910934419844d1cf5b6fe2252beed88" title="Computes the Binary Cross-Entropy (BCE) loss in the forward pass.">forward</a> pass. </dd>
<dd>
<a class="el" href="#a8e684013f81ef2431f2afcb551d00b0e" title="Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the b...">backward()</a> for gradient computation in the <a class="el" href="#a8e684013f81ef2431f2afcb551d00b0e" title="Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the b...">backward</a> pass.</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/07 </dd></dl>

<p class="definition">Definition at line <a class="el" href="_nodes_8cuh_source.html#l03242">3242</a> of file <a class="el" href="_nodes_8cuh_source.html">Nodes.cuh</a>.</p>
</div><h2 class="groupheader">Constructor &amp; Destructor Documentation</h2>
<a id="a909fb24c4dac76c49910cdd7cfce7f00" name="a909fb24c4dac76c49910cdd7cfce7f00"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a909fb24c4dac76c49910cdd7cfce7f00">&#9670;&#160;</a></span>BinaryCrossEntropyNode()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode::BinaryCrossEntropyNode </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html">Node</a> *</td>          <td class="paramname"><span class="paramname"><em>input1</em></span>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html">Node</a> *</td>          <td class="paramname"><span class="paramname"><em>input2</em></span>&#160;)</td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">explicit</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Constructor to initialize a <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node.html" title="Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph.">BinaryCrossEntropyNode</a></code> for computing the Binary Cross-Entropy loss. </p>
<p>The constructor initializes a <code><a class="el" href="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node.html" title="Represents the Binary Cross-Entropy (BCE) loss function node in a computational graph.">BinaryCrossEntropyNode</a></code>, which applies the Binary Cross-Entropy loss function to two input tensors. It verifies that both input tensors have the same shape and establishes a connection in the computational graph by storing the second input tensor. The node's type is set to "BinaryCrossEntropy".</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">input1</td><td>A pointer to the first input node. This tensor represents the predicted probabilities. </td></tr>
    <tr><td class="paramname">input2</td><td>A pointer to the second input node. This tensor represents the true binary labels (0 or 1).</td></tr>
  </table>
  </dd>
</dl>
<dl class="exception"><dt>Exceptions</dt><dd>
  <table class="exception">
    <tr><td class="paramname">std::invalid_argument</td><td>If the shapes of the two input tensors do not match.</td></tr>
  </table>
  </dd>
</dl>
<ul>
<li>This constructor ensures that the input tensors have the same shape; otherwise, an exception is thrown.</li>
<li>The first input tensor is connected to the output node, while the second input tensor is added to the <code>inputs</code> vector.</li>
<li>The node's type is set to "BinaryCrossEntropy" to reflect its operation.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>The two input tensors must have the same shape, as the Binary Cross-Entropy loss is computed element-wise.</li>
</ul>
</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/07 </dd></dl>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00525">525</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>

</div>
</div>
<h2 class="groupheader">Member Function Documentation</h2>
<a id="a8e684013f81ef2431f2afcb551d00b0e" name="a8e684013f81ef2431f2afcb551d00b0e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a8e684013f81ef2431f2afcb551d00b0e">&#9670;&#160;</a></span>backward()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode::backward </td>
          <td>(</td>
          <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">override</span><span class="mlabel">virtual</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the backward pass. </p>
<p>This method computes the gradients of the Binary Cross-Entropy loss with respect to both input tensors (<code>input1</code> and <code>input2</code>). The gradients are computed only if the output tensor requires gradients (i.e., during the backpropagation process). The gradients are propagated back to the input nodes to update their weights during training.</p>
<p>The gradient of Binary Cross-Entropy with respect to the predicted probabilities (<code>y_pred</code>) is computed as: </p><div class="fragment"><div class="line">dBCE/dy_pred = - ( y_true / y_pred ) + ( (1 - y_true) / (1 - y_pred) )</div>
</div><!-- fragment --><p> where <code>y_pred</code> is the predicted probability and <code>y_true</code> is the true binary label (0 or 1).</p>
<p>The gradient computation is parallelized on the GPU using CUDA, enabling efficient backpropagation even with large datasets.</p>
<ul>
<li>A kernel is launched to compute the gradients in parallel for all elements in the tensors.</li>
<li>The gradients are stored in the <code>grad</code> attribute of the output tensor, which is propagated to the input nodes during backpropagation.</li>
<li>The backward pass is only executed if the output tensor has <code>requiresGrad()</code> set to true, ensuring that gradients are computed only when necessary.</li>
<li>The method uses GPU memory for efficient computation and returns the results to the host after calculation.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>This method assumes that both input tensors have the same shape. The gradients are computed element-wise and are propagated back to the input tensors accordingly.</li>
<li>The gradients with respect to the predicted probabilities (<code>y_pred</code>) are accumulated in the <code>grad</code> attribute of the output tensor.</li>
<li>If the output does not require gradients, this method does nothing.</li>
</ul>
</dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="#a4910934419844d1cf5b6fe2252beed88" title="Computes the Binary Cross-Entropy (BCE) loss in the forward pass.">forward()</a> for the BCE loss computation in the <a class="el" href="#a4910934419844d1cf5b6fe2252beed88" title="Computes the Binary Cross-Entropy (BCE) loss in the forward pass.">forward</a> pass.</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/07 </dd></dl>

<p>Implements <a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html#a41914155871c84330701f9d1649b39f3">NeuZephyr::Nodes::Node</a>.</p>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00553">553</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>
<div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a8e684013f81ef2431f2afcb551d00b0e_cgraph.png" border="0" usemap="#aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a8e684013f81ef2431f2afcb551d00b0e_cgraph" alt=""/></div>
<map name="aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a8e684013f81ef2431f2afcb551d00b0e_cgraph" id="aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a8e684013f81ef2431f2afcb551d00b0e_cgraph">
<area shape="rect" title="Computes the gradients of the Binary Cross&#45;Entropy (BCE) loss with respect to the inputs during the b..." alt="" coords="5,5,183,64"/>
<area shape="rect" href="namespace_neu_zephyr_1_1_kernels.html#aee2f66059d96f9382426b606ece8f699" title="Kernel function to compute the gradient of Binary Cross Entropy (BCE) loss for backpropagation." alt="" coords="231,13,368,56"/>
<area shape="poly" title=" " alt="" coords="183,32,215,32,215,37,183,37"/>
</map>
</div>

</div>
</div>
<a id="a4910934419844d1cf5b6fe2252beed88" name="a4910934419844d1cf5b6fe2252beed88"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a4910934419844d1cf5b6fe2252beed88">&#9670;&#160;</a></span>forward()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void NeuZephyr::Nodes::Loss::BinaryCrossEntropyNode::forward </td>
          <td>(</td>
          <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">override</span><span class="mlabel">virtual</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Computes the Binary Cross-Entropy (BCE) loss in the forward pass. </p>
<p>This method computes the Binary Cross-Entropy loss between the predicted probabilities (from the first input tensor) and the true binary labels (from the second input tensor). The loss is calculated element-wise and accumulated. The result is stored in the <code>loss</code> attribute, which can be accessed after the forward pass.</p>
<p>The Binary Cross-Entropy loss is computed as: </p><div class="fragment"><div class="line">BCE(y_pred, y_true) = - ( y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred) )</div>
</div><!-- fragment --><p> where <code>y_pred</code> is the predicted probability and <code>y_true</code> is the true label (0 or 1).</p>
<p>The calculation is done in parallel on the GPU using CUDA to handle large tensor sizes efficiently.</p>
<ul>
<li>The forward pass involves allocating memory on the GPU, performing the BCE loss computation, and accumulating the loss.</li>
<li>A kernel is launched with a grid of threads to compute the loss across all elements of the tensors.</li>
<li>The result is copied back to the host memory, where the accumulated loss is added to the <code>loss</code> attribute.</li>
<li>After computation, the allocated GPU memory is freed.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>This method assumes that both input tensors have the same shape. The computation is performed element-wise, and the tensors must be compatible for this operation.</li>
<li>The <code>loss</code> attribute will hold the accumulated Binary Cross-Entropy loss after the forward pass.</li>
</ul>
</dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="#a8e684013f81ef2431f2afcb551d00b0e" title="Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the b...">backward()</a> for the gradient computation in the <a class="el" href="#a8e684013f81ef2431f2afcb551d00b0e" title="Computes the gradients of the Binary Cross-Entropy (BCE) loss with respect to the inputs during the b...">backward</a> pass.</dd></dl>
<dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/12/07 </dd></dl>

<p>Implements <a class="el" href="class_neu_zephyr_1_1_nodes_1_1_node.html#a64e42ba40199e35bfe453ef14b2d15c0">NeuZephyr::Nodes::Node</a>.</p>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00534">534</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>
<div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="class_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a4910934419844d1cf5b6fe2252beed88_cgraph.png" border="0" usemap="#aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a4910934419844d1cf5b6fe2252beed88_cgraph" alt=""/></div>
<map name="aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a4910934419844d1cf5b6fe2252beed88_cgraph" id="aclass_neu_zephyr_1_1_nodes_1_1_loss_1_1_binary_cross_entropy_node_a4910934419844d1cf5b6fe2252beed88_cgraph">
<area shape="rect" title="Computes the Binary Cross&#45;Entropy (BCE) loss in the forward pass." alt="" coords="5,5,183,64"/>
<area shape="rect" href="namespace_neu_zephyr_1_1_kernels.html#a03a44febc6abfdac6df08b9fb33e6e03" title="Kernel function to compute the Binary Cross Entropy (BCE) loss between predicted and real values." alt="" coords="231,13,377,56"/>
<area shape="poly" title=" " alt="" coords="183,32,215,32,215,37,183,37"/>
</map>
</div>

</div>
</div>
<hr/>The documentation for this class was generated from the following files:<ul>
<li>D:/C Program/Simple-CPP-DL-Framework/include/NeuZephyr/<a class="el" href="_nodes_8cuh_source.html">Nodes.cuh</a></li>
<li>D:/C Program/Simple-CPP-DL-Framework/src/<a class="el" href="_nodes_8cu_source.html">Nodes.cu</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0
</small></address>
</div><!-- doc-content -->
</body>
</html>
